package cn.njyd.corejava.thread.myday04;

import java.util.Arrays;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ThreadLocalRandom;

public class CyclicBarrierDemo01 {
    public static void main(String[] args) {
        String[] names = {"敬业","发财","幸运","工作","事业"};
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5,()->{
            System.out.println("完成："+ Arrays.toString(names));
            int money = ThreadLocalRandom.current().nextInt(100);
            System.out.println("参与抽奖"+money+"活动结束");

        });

        for (int i=1;i<=5;i++) {
            new Thread(()->{
                try{
                    System.out.println("恭喜抽到"+names[Integer.valueOf(Thread.currentThread().getName())-1]);
                }finally {
                    try {
                        cyclicBarrier.await();
//                        System.out.println("活动结束");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            },String.valueOf(i)).start();
        }

    }
}
